/**
 * Created by yvdedu.com on 2016/10/27.
 */

//评论相关功能
!function () {



    var commentPTarget=$(".comment");

    var myid=0;
    var id=0;
    //加载完成后执行
    $(function () {
        getRemarkData();
        getPrise();
        //todo:是否已赞
    });
    var page=1;
    $(window).scroll(function () {
        if($(window).height() + $(window).scrollTop()>=document.scrollingElement.scrollHeight&&$(window).scrollTop()>0){
            var guest=$(".comment .guestModel");
            if(guest.length!=0&&guest.length%30==0){
                $.post("/getRemarkData",{page:++page,id:id},function(o){
                    if(o){
                        var jsarr=JSON.parse(o);
                        for(var i=0;i<jsarr.length;++i){
                            setOneDate(jsarr[i]);
                        }
                    }
                })
            }
        }
    });


    //获取评论数据!!!!!
    function getRemarkData() {
        id=guestSubmitTarget.attr("id");
        $.post("/getMineInfo",function (uid) {
            if(uid){
                myid=JSON.parse(uid).id;
            }
            $.post("/getRemarkData",{id:id,page:1},function (o) {
                if(o){
                    var js=JSON.parse(o);
                    setDate(js,myid);
                }
            })
        });
    }



    var priseButtonTarget=$(".priseButton");
    //赞或者取消赞

    function tojson(s){
        return JSON.parse(s);
    }
    function setPrise(){
        var id=guestSubmitTarget.attr("id");
        $.post("/setPrise",{id:id},function(o){
            var js=tojson(o);
            setPriseData(js);
        })
    }

    //获取赞数据
    function getPrise(){
        var id=guestSubmitTarget.attr("id");
        $.post("/getPrise",{id:id},function(o){
            if(o){
                var js=tojson(o);
                setPriseData(js);
            }
        })
    }

    //赞控件操作
    function setPriseData(js){
        priseButtonTarget.text((js.prise?"已赞":"赞")+"（"+js.num+"）");
    }
    priseButtonTarget.click(setPrise);


    //转发
    var forwardButtonTarget=$(".forwardButton");
    forwardButtonTarget.click(forward);
    function forward(){
        var id=guestSubmitTarget.attr("id");
        $.post("/forward",{id:id},function(o){
            console.log(o);
            if(o!=0){
                location.href="/Article"+o;
            }
        })
    }





    function setOneDate(j){
        var time=new Date(j.createTime).toLocaleString();
        var temp=guestModelTarget.clone(true);
        temp.find(".guest").text(123);
        temp.find(".authorPhoto").attr("src",j.user.photo?j.user.photo:"photo/0.png");
        temp.find(".guest").text(j.user.nickName?j.user.nickName:j.user.name);
        temp.find(".guest").attr("href","user"+j.user.id);
        temp.find(".commentTextContent").text(j.content);
        temp.find(".guestTime").text(time);
        if(j.user.id==myid||j.article.user.id==myid){
            temp.find(".delGuest").attr("id",j.id);
        }else {
            temp.find(".delGuest").remove();
        }
        commentPTarget.append(temp);
        temp.css("display","block")
    }

    //设置评论数据
    function setDate(js) {
        clearDate();
        for (var i=0;i<js.length;++i){
            setOneDate(js[i]);
        }
    }
//清除所有评论
    function clearDate() {
        $(".comment .guestModel").remove();
        ArticleCommentIptTarget.val("");
        setCount(0);
    }





    var textCountTarget=$(".textCount");
    var ArticleCommentIptTarget=$(".ArticleCommentIpt");
    var guestSubmitTarget=$(".guestSubmit");
    var guestModelTarget=$(".guestModel");

    var delGuest=$(".delGuest");


//字数提示功能
    function setContent(text) {
        ArticleCommentIptTarget.val(text);
    }
    function setCount(i) {
        textCountTarget.text("("+i+"/250)");
    }
    function showCount() {
        var text=ArticleCommentIptTarget.val();
        var count=text.length;
        if (count>250){
            text=text.substr(0,250);
            setContent(text)
            setCount(250);
        }else {
            setCount(count);
        }
    }


//提交评论
    function submit() {
        var id=guestSubmitTarget.attr("id");
        var text=ArticleCommentIptTarget.val();
        $.post("/remark",{id:id,content:text},function (o) {
            console.log(o);
            if (o==true){
                getRemarkData();
            }else {
                if($(".loginButton").length>0){
                    $(".loginButton").click();
                }else {
                    location.reload(true);
                }
            }
        })
    }
//删除评论
    function del() {
        var id=$(this).attr("id");
        $.post("/delRemark",{id:id},function (o) {
            console.log(o)
            if(o==true){
                getRemarkData();
            }
        })
    }



    function down() {
        $(this).addClass("buttonPress");
    }
    function up() {
        $(this).removeClass("buttonPress");
    }


    ArticleCommentIptTarget.keydown(showCount);
    ArticleCommentIptTarget.keyup(showCount);
    guestSubmitTarget.click(submit);
    guestSubmitTarget.mousedown(down);
    guestSubmitTarget.mouseup(up);
    delGuest.click(del);
    delGuest.mousedown(down);
    delGuest.mouseup(up);
}();